package com.pzhu.record.interceptor;

import com.pzhu.record.entity.Course;
import com.pzhu.record.entity.Institute;
import com.pzhu.record.service.CourseService;
import com.pzhu.record.service.InstituteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Zhou YuSen
 * @date 2020/4/20 14:38
 */
@Component
public class MyHandlerInterceptor implements HandlerInterceptor {
    @Autowired
    private CourseService courseService;
    @Autowired
    private InstituteService instituteService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        List<Course> courseList = courseService.selectAll();
        List<Course> courseYear = courseService.selectAllYear();
        List<Institute> instituteList = instituteService.selectAll();
        httpServletRequest.getServletContext().setAttribute("courseList", courseList);
        httpServletRequest.getServletContext().setAttribute("courseYear", courseYear);
        httpServletRequest.getServletContext().setAttribute("instituteList", instituteList);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
